package tech.aichan.AiQA.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @author mengyu
 * @date 2024/05/29 16:18
 */
@Configuration
@Slf4j
public class CaffeineConfig {

    @Value("${cache.time}")
    private Integer TIME;
    @Value("${cache.size}")
    private Integer SIZE;

    @Bean
    public Cache<String,byte[]> cache(){
        RemovalListener<String,byte[]> removalListener = (key, value, cause) -> {
            log.info("remove key:{}, cause:{}",key,cause);
        };
        return Caffeine.newBuilder()
                .expireAfterWrite(TIME, TimeUnit.MINUTES)
                .maximumSize(SIZE)
                .recordStats()
                .removalListener(removalListener)
                .build();
    }
}
